package com.chuantou.yuba.ui;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.chuantou.yuba.R;
import com.chuantou.yuba.api.RequestManager;
import com.chuantou.yuba.api.request.GsonRequest;
import com.chuantou.yuba.api.to.BaseTO;
import com.chuantou.yuba.api.to.UserTO;
import com.chuantou.yuba.util.AccountManager;
import com.chuantou.yuba.util.Constants;
import com.chuantou.yuba.util.UiUtil;

/**
 * 修改昵称界面
 * 
 * @author dl1001
 * 
 */
public class EditNicknameActivity extends BaseActionBarActivity {
	private EditText mNicknameView;
	private ProgressDialog mProgressDialog;
	private UserTO mUser;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_nickname);
		mUser = AccountManager.getInstance(getApplicationContext()).getUserTO();
		if (mUser == null) {
			finish();
			return;
		}
		setupViews();
		setTitle(R.string.nickname);
	}

	private void setupViews() {
		mProgressDialog = new ProgressDialog(this);
		mProgressDialog.setMessage(getString(R.string.uploading));
		mProgressDialog.setCancelable(false);
		mNicknameView = (EditText) findViewById(R.id.nickname);
		mNicknameView.setText(mUser.getUsername());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_save, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.menu_save) {
			if (UiUtil.isFastDoubleClick())
				return true;
			String nickname = mNicknameView.getText().toString().trim();
			if (TextUtils.isEmpty(nickname) || nickname.length() < 2) {
				Toast.makeText(getApplicationContext(), R.string.input_nickname_error, Toast.LENGTH_SHORT).show();
				return true;
			}
			updateNickname(mNicknameView.getText().toString());
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private void updateNickname(final String nickname) {
		if (mUser.isExpired()) {
			Toast.makeText(this, R.string.message_token_expired, Toast.LENGTH_SHORT).show();
			LoginActivity.start(this, -1, LoginActivity.ACTION_RETURN);
		} else {
			GsonRequest<BaseTO> request = RequestManager.getUpdateNicknameRequest(new Listener<BaseTO>() {

				@Override
				public void onResponse(BaseTO response) {
					if (!hasDestroyed()) {
						mProgressDialog.dismiss();
						if (response == null) {
							Toast.makeText(getApplicationContext(), R.string.update_failed, Toast.LENGTH_SHORT).show();
						} else {
							if (response.isSuccess()) {
								Toast.makeText(getApplicationContext(), R.string.update_success, Toast.LENGTH_SHORT).show();
								Intent data = new Intent();
								data.putExtra(Constants.EXTRA_DATA, nickname);
								setResult(RESULT_OK, data);
								finish();
							} else if (response.getStatus() == BaseTO.STATUS_TOKEN_ERROR || response.getStatus() == BaseTO.STATUS_TOKEN_EXPIRED) {
								Toast.makeText(getApplicationContext(), R.string.message_token_expired, Toast.LENGTH_SHORT).show();
								LoginActivity.start(EditNicknameActivity.this, -1, LoginActivity.ACTION_RETURN);
							} else {
								String msg = response.getMsg();
								msg = TextUtils.isEmpty(msg) ? getString(R.string.update_failed) : msg;
								Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
							}
						}
					}
				}
			}, new ErrorListener() {

				@Override
				public void onErrorResponse(VolleyError error) {
					if (!hasDestroyed()) {
						mProgressDialog.dismiss();
						Toast.makeText(getApplicationContext(), R.string.request_error, Toast.LENGTH_SHORT).show();
					}
				}
			}, mUser, nickname);
			mProgressDialog.show();
			RequestManager.startRequest(getApplicationContext(), request);
		}
	}
}
